package com.mokee.android.Fragment;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mokee.android.ShipPass.R;
import com.mokee.android.ShipPass.ShipProcessListActivity;
import com.mokee.android.database.DataBaseHelper;
import com.mokee.android.database.UserRecord;
import com.mokee.android.database.UserTable;

public class PayFragment extends Fragment implements OnClickListener {
	private static final String tag = "PayFragment";
	
	private LinearLayout layout_NewPay, layout_StartPay, layout_ReEdit, layout_BanPass, layout_Success;
	private TextView tv_NewNum, tv_PayNum, tv_ReEditNum, tv_BanNum, tv_SuccessNum;

	private int newNum = 0, payNum = 0, reEditNum = 0, banNum = 0, successNum = 0;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		
		View view = inflater.inflate(R.layout.activity_pay, container, false);
		initView(view);
		initEvent();
		initData();
		return view;
	}

	private void initView(View view) {
		layout_NewPay = (LinearLayout) view.findViewById(R.id.layout_NewPay);
		layout_StartPay = (LinearLayout) view.findViewById(R.id.layout_StartPay);
		layout_ReEdit = (LinearLayout) view.findViewById(R.id.layout_ReEdit);
		layout_BanPass = (LinearLayout) view.findViewById(R.id.layout_BanPass);
		layout_Success = (LinearLayout) view.findViewById(R.id.layout_Success);
		
		tv_NewNum = (TextView) view.findViewById(R.id.tv_NewNum);
		tv_PayNum = (TextView) view.findViewById(R.id.tv_PayNum);
		tv_ReEditNum = (TextView) view.findViewById(R.id.tv_ReEditNum);
		tv_BanNum = (TextView) view.findViewById(R.id.tv_BanNum);
		tv_SuccessNum = (TextView) view.findViewById(R.id.tv_SuccessNum);
	}
	
	private void initEvent() {
		layout_NewPay.setOnClickListener(this);
		layout_StartPay.setOnClickListener(this);
		layout_ReEdit.setOnClickListener(this);
		layout_BanPass.setOnClickListener(this);
		layout_Success.setOnClickListener(this);
	}
	
	private void initData() {
		newNum = 0;
		payNum = 0;
		reEditNum = 0;
		banNum = 0;
		successNum = 0;
		
		UserRecord record = DataBaseHelper.getInstance().getUserRecord();
		List<UserTable> userList = record.queryAllUser();
		Log.i(tag, "userList:" + userList);
		if(userList != null && userList.size() > 0){
			for(UserTable item : userList){
				loadItemNum(item);
			}
		}
		tv_NewNum.setText(String.valueOf(newNum));
		tv_PayNum.setText(String.valueOf(payNum));
		tv_ReEditNum.setText(String.valueOf(reEditNum));
		tv_BanNum.setText(String.valueOf(banNum));
		tv_SuccessNum.setText(String.valueOf(successNum));
		
		Log.i(tag, "每项的数量：New:" + newNum + ",Pay:" + payNum + ",ReEdit:" + reEditNum + ",Ban:" + banNum + ",Success:" +successNum);
	}

	private void loadItemNum(UserTable item) {
		switch (item.getShipProcess()) {
		case 0:
			newNum++;
			break;
		case 1:
			payNum++;
			break;
		case 2:
			reEditNum++;
			break;
		case 3:
			banNum++;
			break;
		case 4:
			successNum++;
			break;
		default:
			break;
		}
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(getActivity(),ShipProcessListActivity.class);
		Bundle data = new Bundle();
		switch (v.getId()) {
		case R.id.layout_NewPay:
			data.putInt("process", 0);
			intent.putExtra("process_bundle", data);
			startActivity(intent);
			break;
		case R.id.layout_StartPay:
			data.putInt("process", 1);
			intent.putExtra("process_bundle", data);
			startActivity(intent);
			break;
		case R.id.layout_ReEdit:
			data.putInt("process", 2);
			intent.putExtra("process_bundle", data);
			startActivity(intent);
			break;
		case R.id.layout_BanPass:
			data.putInt("process", 3);
			intent.putExtra("process_bundle", data);
			startActivity(intent);
			break;
		case R.id.layout_Success:
			data.putInt("process", 4);
			intent.putExtra("process_bundle", data);
			startActivity(intent);
			break;
		default:
			break;
		}
	}
}
